package com.wan.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences.Editor;
import android.view.KeyEvent;
import android.widget.Toast;

import java.util.HashMap;

import org.json.JSONObject;

import com.wan.commonsdk.Conet;

public class BaseActivity extends Activity {
	private ProgressDialog progressDialog = null;
	private HashMap<String, String> hint;

	// ------------------------------------------------------------------------
	// 初始化类
	// 所有的activity都继承此类
	// hint为后端返回的结果合集
	//
	// ------------------------------------------------------------------------
	public BaseActivity() {
		hint = new HashMap<String, String>();
		hint.put("501", "您没有输入用户名");
		hint.put("502", "您没有输入密码");
		hint.put("503", "您没有输入客户端ID");
		hint.put("504", "您没有输入游戏");
		hint.put("201", "用户名格式错误");
		hint.put("202", "密码格式错误");
		hint.put("203", "您的输入原密码有误，请确认后再输入");
		hint.put("900", "签名错误");
		hint.put("-1", "由于网络原因，您的当前操作失败，请稍后再试");
		hint.put("0005", "帐号不存在");
		hint.put("0006", "您输入的密码不正确");
		hint.put("100", "用户名己经存在");
		hint.put("700", "您没有输入ID");
		hint.put("701", "您没有输入正确的电话号码");
		hint.put("702", "您的签名错误");
		hint.put("703", "默认发送间隔时间为30秒，请稍后再试");
		hint.put("704", "发送信息失败，请稍后再试");
		hint.put("705", "您输入的手机号与绑定的手机号不一致");
		hint.put("706", "您没有绑定手机，无法找回密码");
		hint.put("800", "您没有输入ID");
		hint.put("801", "验证码错误");
		hint.put("802", "您没有输入正确的电话号码");
		hint.put("803", "您没有输入客户端ID");
		hint.put("804", "绑定失败，请稍后再试");
		hint.put("805", "您己经绑定过了");
		hint.put("806", "验证失败，请确认验证码是否输入正确或是已经过期");
		hint.put("9000", "操作成功");
		hint.put("4000", "系统异常");
		hint.put("4001", "数据格式不正确");
		hint.put("4003", "该用户绑定的支付宝账户被冻结或不允许支付");
		hint.put("4004", "该用户已解除绑定");
		hint.put("4005", "绑定失败或没有绑定");
		hint.put("4006", "订单支付失败");
		hint.put("4010", "重新绑定账户");
		hint.put("6000", "支付服务正在进行升级操作");
		hint.put("6001", "用户中途取消支付操作");
		hint.put("7001", "网页支付失败");

	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
//		TCAgent.onResume(this);
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
//		TCAgent.onPause(this);
	}
	// ------------------------------------------------------------------------
	// 显示返回信息
	// 根据code,从hint中找到返回信息，并显示给用户
	// hint为后端返回的结果合集
	// ------------------------------------------------------------------------
	protected void showToast(String code) {
		if (hint.get(code) != null) {
			Toast.makeText(getApplicationContext(), hint.get(code).toString(),
					Toast.LENGTH_SHORT).show();
		}
	}

	// ------------------------------------------------------------------------
	// 显示消息类
	// 给用户显示content的内容
	// ------------------------------------------------------------------------
	protected void showHint(String content) {
		Toast.makeText(getApplicationContext(), content.toString(),
				Toast.LENGTH_SHORT).show();
	}

	protected void onDestory() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

	protected void showLoading() {
		progressDialog = new ProgressDialog(this);
		progressDialog.setMessage("loading...");
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(false);
		progressDialog.show();
	}

	protected void showLoading(String tips) {
		progressDialog = new ProgressDialog(this);
		progressDialog.setMessage(tips);
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(false);
		progressDialog.show();
	}

	protected void hideLoading() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			return false;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	@Override
	protected void onDestroy() {
		try {
			Editor edit = Conet.WANSP.edit();
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("user_id", Conet.mUserInfo.getUser_id());
			jsonObject.put("access_token", Conet.mUserInfo.getAccess_token());
			jsonObject.put("user_name", Conet.mUserInfo.getUser_name());
			jsonObject.put("user_password", Conet.mUserInfo.getUser_password());
			jsonObject.put("isGuest", Conet.mUserInfo.isGuest());
			jsonObject.put("bind_Phone", Conet.mUserInfo.getBind_Phone());
			jsonObject.put("auto_login", Conet.mUserInfo.isAuto_login());
			edit.putString(Conet.SPUSER, jsonObject.toString());
			edit.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.onDestroy();
	} 
}
